<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;
use App\Http\Models\OrderModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Validator;
use Session;
use App\Http\Models\ExpressModel;
use App\Http\Models\UserModel;
use App\Http\Models\AdvModel;
use App\Http\Models\ArticleModel;


use Overtrue\Wechat\Payment;
use Overtrue\Wechat\Payment\Order;
use Overtrue\Wechat\Payment\Business;
use Overtrue\Wechat\Payment\UnifiedOrder;
use Overtrue\Wechat\Js;

class HomeController extends Controller
{
    public function index(Request $request)
    {
        dd('111');
    }

    public function show(Request $request)
    {
        if (!$request->session()->get('user')) {
            return redirect('/user/login');
        }
        $this->add_userinfo($request->session()->get('user'));

        $adv = AdvModel::all()->toArray();

        $artical = new ArticleModel();

        $we_action = $artical->category_article(4);

        $activity_forecast = $artical->category_article(8);

        $woods_about = $artical->category_article(9);

        $adv_one = AdvModel::find(10)->toArray();
        return view('home.show', compact('adv', 'we_action', 'adv_one', 'activity_forecast', 'woods_about'));

    }

    public function show_article(Request $request, $id)
    {
        $data = ArticleModel::find($id);
        return view('home.article', compact('data'));
    }

    public function show_article_cishan(Request $request, $id)
    {
        $data = ArticleModel::find($id);
        return view('home.articancishan', compact('data'));
    }

    public function cishan()
    {
        $artical = new ArticleModel();
        $data = $artical->category_article(10);
        $data_2 = $artical->category_article(11);
        return view('home.cishan', compact('data', 'data_2'));
    }

    public function action()
    {
        $adv = AdvModel::all()->toArray();
        $artical = new ArticleModel();
        $action = $artical->category_article(4);
        return view('home.action', compact('adv', 'action'));
    }

    public function shanlin()
    {
        $people =OrderModel::where('status',1)->orderBy('create_time','desc') ->take(10)
            ->get()->toArray();

        $user=array();
        foreach ( $people as $key => $value){
           $info= UserModel::where('openid',$value['openid'])->first()->toArray();
            $info['create_time']=$value['create_time'] ;
            $user[]= $info;
        }

        $max_people=UserModel::orderBy('tree_day','desc')->take(10)
            ->get()->toArray();

        return view('home.shanlin', compact('user','max_people'));
    }

    public function xingshan()
    {
        $appId = env('WX_ID');
        $secret = env('WX_SEC');
        $js = new Js($appId, $secret);
        $config=$js->config(array('onMenuShareQQ', 'onMenuShareAppMessage'), true, true);
        $config=json_decode( $config,true);
        $data = ExpressModel::all();
        return view('home.xingshan', compact('data'))->with(['config' => $config]);
    }


    public function selfshan(Request $request)
    {
        if (!$request->session()->get('user')) {
            return redirect('/user/login');
        }
        $appId = env('WX_ID');
        $secret = env('WX_SEC');
        $js = new Js($appId, $secret);
        $config=$js->config(array('onMenuShareQQ', 'onMenuShareAppMessage'), true, true);
        $config=json_decode( $config,true);
        $data = $request->session()->get('user');
        $data = UserModel::Where('openid', $data['openid'])->first();
        if(!$data){
            $data['tree_day']=0 ;
        }
        //判断善行距下一个阶段还有多少天
        $dayinfo = ExpressModel::where('day', '>', $data['tree_day'])->first();

        $data['next_day'] =$dayinfo->day - $data['tree_day'] ;

        return view('home.selfshan', compact('data', 'dayinfo'))->with(['config' => $config]);
    }
    public function  test(){
        $appId = env('WX_ID');
        $secret = env('WX_SEC');
        $js = new Js($appId, $secret);
        $config=$js->config(array('onMenuShareQQ', 'onMenuShareAppMessage'), true, true);
        $config=json_decode( $config,true);
        return view('home.test')->with(['config' => $config]);

    }


    public function pay(Request $request, $price)
    {

        return view('home.pay', ['price' => $price]);
    }

    public function addorder(Request $request ,$price)
    {
        //配置参数
        $data = $request->session()->get('user');
        $business = new Business(
            env('WX_ID'),
            env('WX_SEC'),
            env('MCH_ID'),
            env('MCH_KEY')
        );
        /**
         * 第 2 步：定义订单
         */
        $order = new Order();
        $order_info= $this->add_order_info($data['openid'], $price) ;
        $order->body = 'test body';
        $order->out_trade_no = $order_info;
        $order->total_fee = $price*100;    // 单位为 “分”, 字符串类型
        $order->openid = $data['openid'];
        $order->notify_url = 'https://'.$_SERVER['HTTP_HOST'].'/payok';

        /**
         * 第 3 步：统一下单
         */
        $unifiedOrder = new UnifiedOrder($business, $order);

        /**
         * 第 4 步：生成支付配置文件
         */
        $payment = new Payment($unifiedOrder);
        $payconfig=$payment->getConfig();
        return  json_decode($payconfig,true);
    }


    public function add_userinfo($info)
    {
        $data = array(
            'openid' => $info['openid'],
            'nickname' => $info['nickname'],
            'create_time' => time(),
            'province' => $info['province'],
            'city' => $info['city'],
            'sex' => $info['sex'],
            'headimgurl' => $info['headimgurl'],
            'status' => 1,
            'is_tixing' => 0
        );
        $user_info = UserModel::where('openid', $info['openid'])->first();
        if (empty($user_info)) {
            UserModel::create($data);
        }
    }

    public function  add_order_info($openid, $price){
        $data=array(
            'openid'=>$openid,
             'out_trade_no'=>md5(uniqid() . microtime()),
             'status'=>0,
            'create_time'=>time(),
            'price'=>$price
        );
       $data= OrderModel::create($data);
        if( $data){
           return  $data['out_trade_no']  ;
        }else{
            return false ;
        }
    }







}
